package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.example.programming.common.Result;
import com.example.programming.sys.mapper.CourseApplicationMapper;
import com.example.programming.sys.entity.CourseApplication;

import org.springframework.web.bind.annotation.RestController;
import com.example.programming.common.tools.BaseController;

/**
 * <p>
 * 课程申请记录表 前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-06-29
 */
@RestController
@RequestMapping("/sys/course-application")
public class CourseApplicationController extends BaseController {
    @Autowired
    private CourseApplicationMapper courseApplicationMapper;

    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<CourseApplication> page = new Page<CourseApplication>(pageNum, pageSize);
        CourseApplication parms = new CourseApplication();
        QueryWrapper<CourseApplication> warpper = new QueryWrapper<CourseApplication>(parms);

        result.setData(courseApplicationMapper.selectPage(page, warpper));
        return result;
    }

    public String getCurrTime(){
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日hh时mm分");
        // new Date()为获取当前系统时间
        System.out.println(df.format(new Date()));
        return df.format(new Date());
    }

    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody CourseApplication entity) {
        Result result = new Result();
        entity.setId(null);
        System.out.println("id:"+entity.getId());
        if (entity.getId() == null) {
            entity.setTime(getCurrTime());
            courseApplicationMapper.insert(entity);
        } else {
            courseApplicationMapper.updateById(entity);
        }
        return result;
    }
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        courseApplicationMapper.deleteBatchIds(deleteIds);
        return result;
    }

    @RequestMapping(method= RequestMethod.POST,value="/staffpage")
    public Result staffpage(
            Integer pageNum,
            Integer pageSize,
            String id,
            String course) {
        Result result = new Result();
        Page<CourseApplication> page = new Page<CourseApplication>(pageNum, pageSize);
        CourseApplication parms = new CourseApplication();
        QueryWrapper<CourseApplication> warpper = new QueryWrapper<CourseApplication>(parms);

        if(id != null && !"".equals(id))
        {
            //列名column要和数据库的一样
            warpper.like("id",id);
        }
        if(course != null && !"".equals(course))
        {
            //列名column要和数据库的一样
            warpper.like("course",course);
        }
        result.setData(courseApplicationMapper.selectPage(page, warpper));
        return result;
    }

}
